import requests
from pyquery import PyQuery as pq
import csv
import re
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
# url3='https://www.badmintoncn.com/eqm.php?a=view&eid=6101&mag_hide_progress=1&themecolor=30BC9D'
# url='https://www.badmintoncn.com/cbo_mobile/eq_more.php?a=eqListMore&&bid=1&cid=1&sid=0&name=&order=last&page=0'
# url2='https://www.badmintoncn.com/eqm.php?a=view&eid=7339&mag_hide_progress=1&themecolor=30BC9D'

headers={
'user-agent': 'Mozilla/5.0 (Linux; Android 10; EML-AL00 Build/HUAWEIEML-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/77.0.3865.120 MQQBrowser/6.2 TBS/045610 Mobile Safari/537.36 MAGAPPX|5.3.0-2.0.0-10432|Android 10 HUAWEI EML-AL00|cboapp|YHhwGmfkdzQDACv7nEzpAHrh|5fddd3dd24481b178d3ebcd2ad30185e|533440fbc7e46a95ef21fa0e4554c250',
'referer':	'https://www.badmintoncn.com/eqm.php?a=list&bid=1&mag_hide_progress=1&themecolor=30BC9D'
}
# resp=requests.get(url=url2,headers=headers)
# print(resp.text)
# f=open('羽毛球拍1.csv',mode='w',encoding='utf-8',newline='')
# csvwrite=csv.writer(f)
# resp=requests.get(url=url,headers=headers)
# doc=pq(resp.text)
# ball = doc('.radius_4').items()
# ball2=doc('.radius_4')
def get_price(eid):
    resp_price = requests.get(url=f'https://www.badmintoncn.com/eqm.php?a=view&m=buy&eid={eid}&mag_hide_progress=1&themecolor=30BC9D', headers=headers)
    doc = pq(resp_price.text)
    price = doc('table[cellspacing="4"][class="table_info"]').items()
    for item in price:
        avg_price = item.find('span[class="bluetext2"]').text().split(' ')[0].split('元')[0]
        ershou_price = item.find('span[class="bluetext2"]').text().split(' ')[1].split('元')[0]
        return [avg_price,ershou_price]
def main(url):
    resp = requests.get(url=url, headers=headers)
    doc = pq(resp.text)
    doc = pq(resp.text)
    ball = doc('.radius_4').items()
    for item in ball:
        # print(item.find('div .pinfang'))
        name = item.find('br').parent().text().split('\n')[0]  # 拍名
        # score=item.find('br').parent().text().split('\n')[1].split('★')[1]
        # print(name)
        score = item.find('br').parent().text().split('\n')[1]  # 评分数据
        if re.match('★', score):
            score = score.split('★')[1]
        else:
            score = '暂无数据'
        eid = item.attr('onclick').split('=')[2].split('&')[0]
        abc = get_price(eid)
        avg_price = abc[0]
        er_price = abc[1]
        urllist = f'https://www.badmintoncn.com/eqm.php?a=view&eid={eid}&mag_hide_progress=1&themecolor=30BC9D'
        resp2 = requests.get(url=urllist, headers=headers)
        doc2 = pq(resp2.text)
        scope1 = doc2(f'.table_info tr:nth-child(1) .bluetext2').items()
        scope2 = doc2(f'.table_info tr:nth-child(2) .bluetext2').items()
        scope3 = doc2(f'.table_info tr:nth-child(3) .bluetext2').items()
        scope4 = doc2(f'.table_info tr:nth-child(4) .bluetext2').items()
        scope5 = doc2(f'.table_info tr:nth-child(5) .bluetext2').items()
        scope6 = doc2(f'.table_info tr:nth-child(6) .bluetext2').items()
        scope7 = doc2(f'.table_info tr:nth-child(7) .bluetext2').items()
        cont = doc2('.table_info[cellspacing="0"][style="border-top:1px solid #f7f7f7"]').items()
        global summary
        intro = doc2('meta[name="description"]')
        band = intro.attr('content').split('羽')[0].split(' ')[0]
        abc = doc2('td[class="smalltext"][align="left"]').items()
        for item in abc:
            want_num = item.text().split('想要')[1].split('用过')[0].strip()
            used_num = item.text().split('用过')[1].split('评价')[0].split('\n')[0].strip()
            # print(used_num)
        for item in scope1:
            the_texture_frame = item.siblings().text()
            # print(the_texture_frame)
        for item in scope2:
            the_texture_pole = item.siblings().text()
            # print(the_texture_pole)
        for item in scope3:
            the_weight = item.siblings().text()
            # print(the_weight)
        for item in scope4:
            the_length = item.siblings().text()
        for item in scope5:
            the_chuxi = item.siblings().text()
        for item in scope6:
            the_rendu = item.siblings().text()
        for item in scope7:
            the_bangshu = item.siblings().text()
        for item in cont:
            summary = item.find('td').text()
        csvwrite.writerow([name, band,score, avg_price, er_price, want_num, used_num,the_bangshu,the_weight,the_texture_frame,the_texture_pole])
# a=0
# for i in range(0,25):
#     url=f'https://www.badmintoncn.com/cbo_mobile/eq_more.php?a=eqListMore&&bid=1&cid=1&sid=0&name=&order=last&page={i}'
#     resp = requests.get(url=url, headers=headers)
#     doc = pq(resp.text)
#     ball = doc('.radius_4').items()
#     # for item in ball:

# for inx, item in enumerate(ball):
        # print(item.find('div .pinfang'))
        # name = item.find('br').parent().text().split('\n')[0]
        # score=item.find('br').parent().text().split('\n')[1].split('★')[1]
    # eid=item.attr('onclick').split('=')[2].split('&')[0]
if __name__ == '__main__':
    f = open('所有羽毛球拍.csv', mode='w', encoding='utf-8', newline='')
    csvwrite = csv.writer(f)
    with ThreadPoolExecutor(50) as t:
        for i in range(0, 142):
            # url=f'https://www.badmintoncn.com/cbo_mobile/eq_more.php?a=eqListMore&&bid=1&cid=1&sid=0&name=&order=last&page={i
            t.submit(main, f'https://www.badmintoncn.com/cbo_mobile/eq_more.php?a=eqListMore&&bid=0&cid=1&sid=0&name=&order=last&page={i}')
            print(f'第{i+1}页完成')



